//
//  DHS2_SearchView_All.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/15.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_SearchView_All.h"
#import "DHS2_SearchAll_ProductsCell.h"
#import "DHS2_SearchAll_BrandsCell.h"
#import "DHS2_SearchAll_NewsCell.h"
#import "DHS2_SearchAll_CommunityCell.h"

@interface DHS2_SearchView_All()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSDictionary *allDic;

@property (assign, nonatomic) NSInteger pageIndex;
@property (strong, nonatomic) NSMutableArray *productsArr;
@property (strong, nonatomic) NSMutableArray *articlesArr;
@property (strong, nonatomic) NSMutableArray *brandsArr;
@property (strong, nonatomic) NSMutableArray *postsArr;

@end

@implementation DHS2_SearchView_All

+ (DHS2_SearchView_All *)shareDHS2_SearchView_All{
    DHS2_SearchView_All *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DHS2_SearchView_All class]) owner:self options:nil] objectAtIndex:0];
    return view;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_SearchAll_ProductsCell" bundle:nil] forCellReuseIdentifier:kDHS2_SearchAll_ProductsCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_SearchAll_BrandsCell" bundle:nil] forCellReuseIdentifier:kDHS2_SearchAll_BrandsCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_SearchAll_NewsCell" bundle:nil] forCellReuseIdentifier:kDHS2_SearchAll_NewsCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_SearchAll_CommunityCell" bundle:nil] forCellReuseIdentifier:kDHS2_SearchAll_CommunityCell];
    self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(searchData)];
    self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(searchMoreData)];
}

//搜索
- (void)searchData{
    self.pageIndex = 0;
    NSString *sex = @"男";
    [self.tableView.mj_footer resetNoMoreData];
    if ([GlobalManager shareGlobalManager].user.sex) {
        sex = [GlobalManager shareGlobalManager].user.sex;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:@"全部" forKey:@"type"];
    [params setObject:self.keyWord forKey:@"key"];
    [params setObject:@(self.pageIndex) forKey:@"pageIndex"];
    [params setObject:@(20) forKey:@"pageSize"];
    [[DHNetAPIClient shareDHNetAPIClient] request_app_searchWithParams:params AndBlock:^(id data, NSError *error) {
        if (data) {
            [self.tableView.mj_header endRefreshing];
            
            self.allDic = [data objectForKey:@"results"];
            [self.postsArr removeAllObjects];
            [self.articlesArr removeAllObjects];
            [self.brandsArr removeAllObjects];
            [self.productsArr removeAllObjects];
            
            [self.postsArr addObjectsFromArray:[self.allDic objectForKey:@"posts"]];
            [self.articlesArr addObjectsFromArray:[self.allDic objectForKey:@"articles"]];
            
//            NSArray *tmpArr = [self.allDic objectForKey:@"brands"];
            [self.brandsArr addObjectsFromArray:[self.allDic objectForKey:@"brands"]];
            
            
            [self.productsArr addObjectsFromArray:[self.allDic objectForKey:@"products"]];
            
            if (self.productsArr.count == 0 && self.brandsArr.count == 0 && self.articlesArr.count == 0 && self.postsArr.count == 0) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            [self.tableView reloadData];
        }
    }];
}

- (void)searchMoreData{
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        if (self.productsArr.count == 0) {
            return 0;
        }else{
            return 46 + 200;
        }
    }else if (indexPath.row == 1){
        if (self.brandsArr.count == 0) {
            return 0;
        }else{
            return 46 + self.brandsArr.count * 65;
        }
    }else if (indexPath.row == 2){
        if (self.articlesArr.count == 0) {
            return 0;
        }else{
            __block CGFloat height = 0;
            [self.articlesArr enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([[obj objectForKey:@"layout"] isEqualToString:@"大矩形"]) {
                    height += 210;
                }else{
                    height += 100;
                }
            }];
            return height + 46;
        }
    }else if (indexPath.row == 3){
        if (self.postsArr.count == 0) {
            return 0;
        }else{
            __block CGFloat totalHeight = 0;
            [self.postsArr enumerateObjectsUsingBlock:^(NSDictionary *model, NSUInteger idx, BOOL * _Nonnull stop) {
                NSArray *images = [model objectForKey:@"images"];
                //计算文字高度
                CGFloat contentHeight = [[model objectForKey:@"content"] heightForFont:[UIFont systemFontOfSize:14] width:kScreenWidth - 20];
                //如果内容中有 \n 等转义字符
                NSInteger specialChar = 0;//出现次数
                specialChar = [[model objectForKey:@"content"] countOccurencesOfString:@"\n"];
                CGFloat textHeight = contentHeight + specialChar * 14;
                
                //图片CollectionView高度
                CGFloat imgsHeight = 0;
                if (images.count == 0) {
                    imgsHeight = 0;
                }else if (images.count > 0 && images.count <= 3){
                    imgsHeight = 128;
                }else if (images.count > 3 && images.count <= 6){
                    imgsHeight = 128 * 2 + 5;
                }else if (images.count > 6 && images.count <= 9){
                    imgsHeight = 128 * 3 + 10;
                }
                CGFloat cellHeight = imgsHeight + textHeight + 133;
                
                totalHeight += cellHeight;
            }];
            return 35 + totalHeight;
        }
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        DHS2_SearchAll_ProductsCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_SearchAll_ProductsCell forIndexPath:indexPath];
        [cell uploadWithArr:self.productsArr];
        if (self.productsArr.count == 0) {
            cell.contentView.hidden = YES;
        }else{
            cell.contentView.hidden = NO;
        }
        return cell;
    }else if (indexPath.row == 1){
        DHS2_SearchAll_BrandsCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_SearchAll_BrandsCell forIndexPath:indexPath];
        [cell updateWithArr:self.brandsArr];
        if (self.brandsArr.count == 0) {
            cell.contentView.hidden = YES;
        }else{
            cell.contentView.hidden = NO;
        }
        return cell;
    }else if (indexPath.row == 2){
        DHS2_SearchAll_NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_SearchAll_NewsCell forIndexPath:indexPath];
        [cell updateWithArr:self.articlesArr];
        if (self.articlesArr.count == 0) {
            cell.contentView.hidden = YES;
        }else{
            cell.contentView.hidden = NO;
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }else if (indexPath.row == 3){
        DHS2_SearchAll_CommunityCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_SearchAll_CommunityCell forIndexPath:indexPath];
        [cell uploadWithArr:self.postsArr];
        if (self.postsArr.count == 0) {
            cell.contentView.hidden = YES;
        }else{
            cell.contentView.hidden = NO;
        }
        return cell;
    }else{
        return nil;
    }
}

#pragma mark - arr
- (NSMutableArray *)postsArr{
    if (!_postsArr) {
        _postsArr = [NSMutableArray array];
    }
    return _postsArr;
}

- (NSMutableArray *)articlesArr{
    if (!_articlesArr) {
        _articlesArr = [NSMutableArray array];
    }
    return _articlesArr;
}

- (NSMutableArray *)brandsArr{
    if (!_brandsArr) {
        _brandsArr = [NSMutableArray array];
    }
    return _brandsArr;
}

- (NSMutableArray *)productsArr{
    if (!_productsArr) {
        _productsArr = [NSMutableArray array];
    }
    return _productsArr;
}

@end
